//
//  HJLineVC.m
//  project
//
//  Created by jc on 2019/5/5.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJLineVC.h"
#import "YFJourneyModel.h"
#import "YFLikeModel.h"
#import "HJChatRoomVC.h"
#import "HJTeamVC.h"
#import "HJLejianCell.h"

@interface HJLineVC ()

/**
 数据源数据
 */
@property (nonatomic, strong) NSArray <YFLikeRecordModel *> *records;
@end
static NSString * const identifier = @"HJLejianCell";
@implementation HJLineVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
}
-(void)configTableView {
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.rowHeight = 97;
    @weakify(self)
    // 上拉刷新，下拉加载
    self.tableView.refresh(^{
        @strongify(self)
        self.page = 1;
        [self loadDatas:self.page];
    })
    .loadMore(^{
        @strongify(self)
        self.page += 1;
        [self loadDatas:self.page];
    });
}
- (void)loadData {
    self.page = 1;
    self.records = [NSArray array];
    [self.tableView.mj_header beginRefreshing];
}

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH-JOURNEY_HEADER_HEIGHT-Height_NavBar-45);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.records.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJLejianCell * lejianCell = [tableView cellForRowAtIndexPath:indexPath];
    if (!lejianCell) {
        lejianCell = [[HJLejianCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [lejianCell loadCellDataWithModel:self.records[indexPath.row]];
    @weakify(self)
    lejianCell.chatBlock = ^(YFLikeRecordModel * _Nullable model) {
        @strongify(self)
        HJChatRoomVC * chatRoomVC = [[UIStoryboard storyboardWithName:@"Chat" bundle:nil]instantiateViewControllerWithIdentifier:chatRoom];
        RCConversationModel * conversationModel = [[RCConversationModel alloc]init];
        conversationModel.conversationTitle = model.user.nickName;
        conversationModel.conversationType = ConversationType_PRIVATE;
        conversationModel.targetId = model.user.userId;
        chatRoomVC.conversationModel = conversationModel;
        [self.navigationController pushViewController:chatRoomVC animated:YES];
    };
    
    lejianCell.peerBlock = ^(YFLikeRecordModel * _Nullable model) {
        LKLog(@"同行");
        @strongify(self)
        HJTeamVC * teamVC = [HJTeamVC new];
        teamVC.recordModel = self.journeyModel;
        teamVC.UIType = UIContentTypeNormal;
        teamVC.otherRecordModel = model;
        [self.navigationController pushViewController:teamVC animated:YES];
    };
    
    return lejianCell;
}


#pragma mark - 网络请求
- (void)loadDatas:(NSInteger)page {
    NSString *postString = [Api stringByAppendingString:TripLikeApi];
    NSDictionary *params = @{
                             @"tripId":_tripId,
                             @"pageCurrent": @(page),
                             @"type": @"3",
                             };
    @weakify(self)
    [HJHttpTool get:postString params:params success:^(HJResponeds *result) {
        @strongify(self)
        YFLikeModel *model = [YFLikeModel yy_modelWithJSON:result.data];
        self.records = self.records.addArray(model.records, self.tableView.mj_header.refreshing);
        [self.tableView reloadData];
        [self.tableView endRefresh];
        if (model.pages == self.page) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        self.tableView.mj_footer.hidden = self.records.count == 0;
    } failure:^(id result) {
        @strongify(self)
        [self.tableView endRefresh];
        if (self.records.count == 0) {
            self.tipEmptyView.message = result;
            [self.tableView reloadData];
        } else {
            [YFHud showError:result];
        }
    }];
}


@end
